﻿using AppDataStruct;
using LogLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace EECMISWS
{
    internal partial class WSClient
    {

        /// <summary>
        /// 查询已办理证件信息
        /// </summary>
        private const string funcQueryPaper = "zj_queryPaper";

        /// <summary>
        /// 全国内地证件查询核查
        /// </summary>
        private const string funcQueryQGPaper = "qg_cn_queryPaper";

        /// <summary>
        /// 查询省内居民证件信息
        /// </summary>
        /// <param name="sfzh">身份证号码</param>
        /// <param name="zjzl">证件种类  HZ SCZ  DLZ</param>
        /// <returns></returns>
        public bool QueryPaper(string sfzh, string zjzl, out PaperInfo paperInfo)
        {
            paperInfo = new PaperInfo();

            JObject jsonResult = new JObject();
            JObject Args = new JObject();
            Args.Add("sfzh", sfzh);
            //Args.Add("xm", "");
            //Args.Add("xb", "");
            //Args.Add("csrq", "");

            zjzl = zjzl.ToUpper();
            if (zjzl == "HZ")
                zjzl = "14";
            else if (zjzl == "SCZ")
                zjzl = "21";
            else if (zjzl == "DLZ")
                zjzl = "25";
            else
            {
                _strMessage = "参数错误";
                Log.WriteAsync(_strMessage);
                return false;
            }

            Args.Add("zjzl", zjzl);
            string json = Args.ToString();
            WebReturnArgs mReturn = new WebReturnArgs();
            mReturn = CallUnimethod(funcQueryPaper, json, true);  //? sq_queryPaper 与文档有出入           
            Log.WriteAsync($"zjzl:[{zjzl}],Recv：data" + "[" + mReturn.DataContent + "]");
            if (!mReturn.CallSuccessed)
            {
                Log.WriteAsync("查询证件信息时发生异常:" + mReturn.ExceptionString);
                return false;
            }
            if (!mReturn.Success)
            {
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (string.IsNullOrEmpty(mReturn.DataContent))
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            JObject jobj = JObject.Parse(mReturn.DataContent);
            if (jobj["papers"] == null)
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            string strInfo = jobj["papers"].ToString();
            if (string.IsNullOrEmpty(strInfo))
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            JArray jarry = JArray.Parse(strInfo);//new JArray(strInfo);
            if (jarry.Count <= 0)
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }

            for (int i = 0; i < jarry.Count; i++)
            {
                if (i != 0 && int.Parse(jarry[i]["qfrq"].ToString()) < int.Parse(paperInfo.qfrq))
                {
                    continue;
                }

                paperInfo.ywxm = jarry[i]["ywxm"].ToString();
                paperInfo.zwxm = jarry[i]["zwxm"].ToString();
                paperInfo.xb = jarry[i]["xb"].ToString();
                paperInfo.csrq = jarry[i]["csrq"].ToString();
                paperInfo.sfzh = jarry[i]["sfzh"].ToString();
                paperInfo.zjzl = jarry[i]["zjzl"].ToString();
                paperInfo.zjhm = jarry[i]["zjhm"].ToString();
                paperInfo.qfrq = jarry[i]["qfrq"].ToString();
                paperInfo.qfjg = jarry[i]["qfjg"].ToString();
                paperInfo.zjyxqz = jarry[i]["zjyxqz"].ToString();
                paperInfo.zjzt = jarry[i]["zjzt"].ToString();
            }

            SaveQueryPaperInfo(paperInfo, zjzl);
            return true;
        }

        /// <summary>
        /// 查询全国内地证件信息
        /// </summary>
        /// <param name="sfzh">身份证号码</param>
        /// <returns></returns>
        public bool QueryQGPaper(string sfzh, out List<CHPaperInfo> listPapers)
        {
            listPapers = new List<CHPaperInfo>();
            JObject jsonResult = new JObject();
            JObject Args = new JObject();
            Args.Add("client_ip", ClientConfig.Client_IP);
            Args.Add("client_zsbh", ClientConfig.Client_ZSBH);
            Args.Add("client_dwbh", ClientConfig.TBDWBH);
            Args.Add("client_dwmc", ClientConfig.Client_DWMC);
            Args.Add("client_xm", ClientConfig.Client_XM);
            Args.Add("client_sfzh", ClientConfig.Client_SFZH);
            //Args.Add("paperNO", "");
            //Args.Add("paperType","");
            Args.Add("idNO", sfzh);
            //Args.Add("name", "");
            //Args.Add("birthday", "");

            string json = Args.ToString();
            WebReturnArgs mReturn = new WebReturnArgs();
            mReturn = CallUnimethod(funcQueryQGPaper, json, true);  //? sq_queryPaper 与文档有出入           
            Log.WriteAsync("Recv：data" + "[" + mReturn.DataContent + "]");
            if (!mReturn.CallSuccessed)
            {
                Log.WriteAsync("查询证件信息时发生异常:" + mReturn.ExceptionString);
                return false;
            }
            if (!mReturn.Success)
            {
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (string.IsNullOrEmpty(mReturn.DataContent))
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            JObject jobj = JObject.Parse(mReturn.DataContent);
            if (jobj["paperInfo"] == null)
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            string strInfo = jobj["paperInfo"].ToString();
            if (string.IsNullOrEmpty(strInfo))
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            string arraySize = jobj["arraySize"].ToString();
            Log.WriteAsync(string.Format("查询{0}得到证件信息数量：{1}", sfzh, arraySize));
            JArray jarry = JArray.Parse(strInfo);//new JArray(strInfo);
            if (jarry.Count <= 0)
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            for (int i = 0; i < jarry.Count; i++)
            {
                CHPaperInfo paperInfo = new CHPaperInfo();
                if (TryParseCHPaperInfo(jarry[i].ToString(), out paperInfo))
                {
                    listPapers.Add(paperInfo);
                    string zjzl = paperInfo.PaperType.code;
                    SaveQueryCHPaperInfo(paperInfo, zjzl);
                }
                else
                {
                    Log.WriteAsync(string.Format("{0}: 解析全国内地证件信息失败。", i));
                }
                //paperInfo.ywxm = jarry[0]["ywxm"].ToString();
                //paperInfo.zwxm = jarry[0]["zwxm"].ToString();
                //paperInfo.xb = jarry[0]["xb"].ToString();
                //paperInfo.csrq = jarry[0]["csrq"].ToString();
                //paperInfo.sfzh = jarry[0]["sfzh"].ToString();
                //paperInfo.zjzl = jarry[0]["zjzl"].ToString();
                //paperInfo.zjhm = jarry[0]["zjhm"].ToString();
                //paperInfo.qfrq = jarry[0]["qfrq"].ToString();
                //paperInfo.qfjg = jarry[0]["qfjg"].ToString();
                //paperInfo.zjyxqz = jarry[0]["zjyxqz"].ToString();
                //paperInfo.zjzt = jarry[0]["zjzt"].ToString();

            }
            return listPapers.Count > 0;
        }

        private bool TryParseCHPaperInfo(string inputStr, out CHPaperInfo info)
        {
            info = new CHPaperInfo();
            try
            {
                info = JsonConvert.DeserializeObject<CHPaperInfo>(inputStr);
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                return false;
            }
        }

        private bool SaveQueryPaperInfo(PaperInfo paperInfo, string zjzl)
        {
            string msg = "";
            bool bRet = Data2Xml.Class2File(paperInfo, BaseDirectory + zjzl + ".xml", out msg);
            if (!bRet)
            {
                Log.WriteAsync("保存查询证件申请信息失败:" + msg);
                return false;
            }
            return true;
        }

        private bool SaveQueryCHPaperInfo(CHPaperInfo paperInfo, string zjzl)
        {
            string msg = "";
            bool bRet = Data2Xml.Class2File(paperInfo, BaseDirectory + zjzl + ".xml", out msg);
            if (!bRet)
            {
                Log.WriteAsync("保存查询证件申请信息失败:" + msg);
                return false;
            }
            return true;
        }

        /// <summary>
        /// 申请信息查询
        /// </summary>
        /// <param name="nERRCode"></param>
        /// <param name="sfzh">身份证号码</param>
        /// <returns></returns>
        public bool QueryApply(string sfzh, ref PreApplyInfo applyInfo)
        {
            JObject Args = new JObject();
            Args.Add("rylb", "R");
            Args.Add("sfzh", sfzh);
            string json = Args.ToString();
            WebReturnArgs mReturn = new WebReturnArgs();
            mReturn = CallUnimethod("sq_queryApply", json, true);
            Log.WriteAsync("Recv：data" + "[" + mReturn.DataContent + "]");
            if (!mReturn.CallSuccessed)
            {
                Log.WriteAsync("查询证件信息时发生异常:" + mReturn.ExceptionString);
                return false;
            }
            if (!mReturn.Success)
            {
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (string.IsNullOrEmpty(mReturn.DataContent))
            {
                _strMessage = "查询证件信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }

            JObject jObj = JObject.Parse(mReturn.DataContent);
            if (jObj["cjsqxxs"] == null)
            {
                _strMessage = "查询证件申请信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            string strInfo = jObj["cjsqxxs"].ToString();
            if (strInfo == "")
            {
                _strMessage = "查询证件申请信息返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }

            JArray jarray = JArray.Parse(strInfo);//new JArray(strInfo);
            if (jarray.Count <= 0)
            {
                _strMessage = "查询证件申请信息为空";
                Log.WriteAsync(_strMessage);
                return false;
            }

            applyInfo.ZWX = jarray[0]["zwx"].ToString();
            applyInfo.ZWM = jarray[0]["zwm"].ToString();
            applyInfo.YWX = jarray[0]["ywx"].ToString();
            applyInfo.YWM = jarray[0]["ywm"].ToString();
            applyInfo.SFZH = sfzh;
            applyInfo.XB = new CodeMap();
            applyInfo.XB.Code = jarray[0]["xb"].ToString();
            applyInfo.XB.Value = applyInfo.XB.Code == "1" ? "男" : "女";
            applyInfo.MZ = new CodeMap();
            applyInfo.MZ.Code = jarray[0]["mz"].ToString();
            applyInfo.MZ.Value = Data2Xml.XmlDicReader(BaseDirectory + "//DICTable//DIC_NATION.xml", applyInfo.MZ.Code);
            applyInfo.CSRQ = jarray[0]["csrq"].ToString();
            applyInfo.CSD = new CodeMap();
            applyInfo.CSD.Code = jarray[0]["csd"].ToString();
            applyInfo.CSD.Value = Data2Xml.XmlDicReader(BaseDirectory + "//DICTable//DIC_PM_BIRTHPLACE.xml", applyInfo.CSD.Code);
            applyInfo.HKSZD = new CodeMap();
            applyInfo.HKSZD.Code = jarray[0]["hkszd"].ToString();
            applyInfo.HKSZD.Value = Data2Xml.XmlDicReader(BaseDirectory + "//DICTable//DIC_CITY.xml", applyInfo.HKSZD.Code);
            applyInfo.LXDH = jarray[0]["lxdh"].ToString();
            applyInfo.JJLXR = jarray[0]["jjqklxr"].ToString();
            applyInfo.JJLXDH = jarray[0]["jjqklxrdh"].ToString();//无
            applyInfo.YJDZ = jarray[0]["emsdz"].ToString();
            applyInfo.YB = jarray[0]["yzbm"].ToString();
            applyInfo.SJR = jarray[0]["sjr"].ToString();
            applyInfo.SJRLXDH = jarray[0]["sjhm"].ToString();
            SavePreApplyInfo(applyInfo);
            return true;
        }

        private bool SavePreApplyInfo(PreApplyInfo applyInfo)
        {
            try
            {
                string msg = "";
                return Data2Xml.Class2File(applyInfo, BaseDirectory + "ApplyInfo.xml", out msg);
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                Log.WriteAsync("保存申请信息查询时发生异常" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 用于查询当前地区是否开通赴港澳或赴台湾个人游
        /// </summary>
        /// <param name="sqlb">申请类别（字典956） 101:护照 102:往来港澳通行证 104:大陆居民往来台湾通行证</param>
        /// <returns>是否开通（1：开通；0：未开通）</returns>
        public bool CheckApplyFDT(string sqlb, out string open)
        {
            open = "0";
            JObject Args = new JObject();

            Args.Add("xzqh", AreaCode);
            Args.Add("sqlb", sqlb);

            string json = Args.ToString();
            WebReturnArgs mReturn = new WebReturnArgs();
            mReturn = CallUnimethod("cn_checkApplyFreedomTour", json, true);

            Log.WriteAsync("Recv：data" + "[" + mReturn.DataContent + "]");
            if (!mReturn.CallSuccessed)
            {
                Log.WriteAsync("查询是否支持个人游时发生异常:" + mReturn.ExceptionString);
                return false;
            }
            if (!mReturn.Success)
            {
                Log.WriteAsync(_strMessage);
                return false;
            }
            if (string.IsNullOrEmpty(mReturn.DataContent))
            {
                _strMessage = "查询是否支持个人游时返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }

            JObject jObj = JObject.Parse(mReturn.DataContent);
            if (jObj["ispass"] == null)
            {
                _strMessage = "查询是否支持个人游时返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            open = jObj["ispass"].ToString();
            if (string.IsNullOrEmpty(open))
            {
                _strMessage = "查询是否支持个人游时返回的data数据为空";
                Log.WriteAsync(_strMessage);
                return false;
            }
            return true;
        }

        private List<string> ListXXLY = new List<string> { "08", "13", "14", "15" };

        /// <summary>
        /// 预受理预约申请信息查询 
        /// </summary>
        /// <param name="sfzh"></param>
        /// <returns></returns> 
        public bool QueryAppointment(string sfzh, out AppointmentInfo appInfo)
        {
            appInfo = new AppointmentInfo();
            try
            {
                JObject Args = new JObject();
                Args.Add("sfzh", sfzh);
                Args.Add("rylb", "R");
                string json = Args.ToString();
                WebReturnArgs mReturn = new WebReturnArgs();
                mReturn = CallUnimethod("sq_queryPreApplyBasic", json, true);
                Log.WriteAsync("预受理基本信息查询Recv：data" + "[" + mReturn.DataContent + "]");
                if (!mReturn.CallSuccessed)
                {
                    Log.WriteAsync("预受理基本信息查询时发生异常:" + mReturn.ExceptionString);
                    return false;
                }
                if (!mReturn.Success)
                {
                    Log.WriteAsync(_strMessage);
                    return false;
                }
                JObject jObj = JObject.Parse(mReturn.DataContent);
                JArray jarray = JArray.Parse(jObj["ndjmxxs"].ToString());
                if (jarray == null || jarray.Count == 0)
                {
                    Log.WriteAsync("查询到预约申请信息为空!");
                    return true;
                }
                // 最新受理日期
                string lastSLRQ = "";
                // 最新申请号
                string lastSQH = "";
                foreach (JObject ndjmxxs in jarray)
                {
                    JObject sq_info = JObject.Parse(ndjmxxs["ndjm"].ToString());
                    if (sq_info["sjslrq"] != null)
                    {
                        string sjslrq = sq_info["sjslrq"].ToString();
                        //实际受理日期,不为空说明已受理
                        if (!string.IsNullOrEmpty(sjslrq))
                        {
                            continue;
                        }
                    }
                    string slrq = sq_info["slrq"].ToString();
                    string sqh = sq_info["sqh"].ToString();
                    string ywbh = sq_info["ywbh"].ToString();
                    string xxly = sq_info["xxly"].ToString();

                    if (string.IsNullOrEmpty(slrq)
                        || string.IsNullOrEmpty(sqh)
                        || string.IsNullOrEmpty(ywbh))
                    {
                        Log.WriteAsync("无效的申请信息，跳过.");
                        continue;
                    }
                    if (!ListXXLY.Contains(xxly))
                    {
                        Log.WriteAsync("来源无效的申请信息，跳过.");
                        continue;
                    }
                    if (string.IsNullOrEmpty(lastSLRQ))
                    {
                        lastSLRQ = slrq;
                    }
                    if (string.IsNullOrEmpty(lastSQH))
                    {
                        lastSQH = sqh;
                    }
                    if (sqh.CompareTo(lastSQH) < 0 || slrq.CompareTo(lastSLRQ) < 0)
                    {
                        Log.WriteAsync("过期无效的申请信息，跳过.");
                        continue;
                    }
                    AppointmentApplyInfo applyInfo = new AppointmentApplyInfo();
                    applyInfo.xxly = xxly;
                    applyInfo.sqh = sqh;
                    applyInfo.slrq = slrq;
                    applyInfo.yzbm = sq_info["yzbm"].ToString();
                    applyInfo.sqlb = sq_info["sqlb"].ToString(); //101护照 102往来港澳通行证 104往来台湾通行证
                    applyInfo.bzlb = sq_info["bzlb"].ToString();
                    applyInfo.ywbh = sq_info["ywbh"].ToString();
                    applyInfo.emsdz = sq_info["emsdz"].ToString();
                    applyInfo.sjr = sq_info["sjr"].ToString();
                    applyInfo.sjhm = sq_info["sjhm"].ToString();
                    applyInfo.jjqklxr = sq_info["jjqklxr"].ToString();
                    applyInfo.jjqklxrdh = sq_info["jjqklxrdh"].ToString();
                    applyInfo.lxdh = sq_info["lxdh"].ToString();
                    applyInfo.sfxqz = sq_info["sfxqz"].ToString();
                    applyInfo.sfxtkzd = sq_info["sfxtkzd"].ToString();
                    applyInfo.sfxxz = sq_info["sfxxz"].ToString();
                    applyInfo.sfzh = sq_info["sfzh"].ToString();
                    applyInfo.zy = sq_info["zy"].ToString();
                    applyInfo.cjsy = sq_info["cjsy"].ToString();
                    applyInfo.sfzh = sq_info["sfzh"].ToString();
                    applyInfo.sldw = sq_info["sldw"].ToString();
                    applyInfo.tbdwbh = sq_info["tbdwbh"].ToString();
                    applyInfo.zwxm = sq_info["zwxm"].ToString();

                    appInfo.AddApplyInfo(applyInfo);

                    if (applyInfo.sqlb == "101"
                        || string.IsNullOrEmpty(applyInfo.sfxqz)
                        || applyInfo.sfxqz == "0")
                    {
                        appInfo.AddApplyInfo(applyInfo);
                        continue;
                    }

                    if (ndjmxxs["qzblxx"] == null)
                    {
                        continue;
                    }
                    JArray qz_arrayinfo = JArray.Parse(ndjmxxs["qzblxx"].ToString());
                    if (qz_arrayinfo == null || qz_arrayinfo.Count == 0)
                    {
                        continue;
                    }
                    foreach (JObject qzblxx in qz_arrayinfo)
                    {
                        AppointmentQZInfo qZInfo = new AppointmentQZInfo();
                        qZInfo.ywbh = qzblxx["ywbh"].ToString();
                        if (applyInfo.ywbh != qZInfo.ywbh)
                        {
                            continue;
                        }
                        qZInfo.qwd = qzblxx["qwd"].ToString();
                        qZInfo.qzzl = qzblxx["qzzl"].ToString();
                        qZInfo.qzblId = qzblxx["qzblId"].ToString();
                        qZInfo.qzyxcs = qzblxx["qzyxcs"].ToString();
                        qZInfo.qzyxq = qzblxx["qzyxq"].ToString();
                        qZInfo.qzyxqdw = qzblxx["qzyxqdw"].ToString();
                        qZInfo.tbdwbh = qzblxx["tbdwbh"].ToString();
                        qZInfo.xh = qzblxx["xh"].ToString();
                        appInfo.AddQZInfo(qZInfo);
                    }

                    if (ndjmxxs["gxrxx"] == null)
                    {
                        continue;
                    }
                    JArray gxr_arrayinfo = JArray.Parse(ndjmxxs["gxrxx"].ToString());
                    if (gxr_arrayinfo == null || gxr_arrayinfo.Count == 0)
                    {
                        continue;
                    }
                    foreach (JObject gxrxx in gxr_arrayinfo)
                    {
                        RelationPeopleInfo gxrInfo = new RelationPeopleInfo();
                        gxrInfo.ywbh = gxrxx["ywbh"].ToString();
                        if (applyInfo.ywbh != gxrInfo.ywbh)
                        {
                            continue;
                        }
                        gxrInfo.zwxm = gxrxx["zwxm"].ToString();
                        gxrInfo.zjhm = gxrxx["zjhm"].ToString();
                        gxrInfo.xb = gxrxx["xb"].ToString();
                        gxrInfo.sfzhm = gxrxx["sfzhm"].ToString();
                        gxrInfo.qsgx = gxrxx["qsgx"].ToString();
                        gxrInfo.zjhm = gxrxx["zjhm"].ToString();
                        gxrInfo.gxrlb = gxrxx["gxrlb"].ToString();
                        appInfo.AddRelationPeopleInfo(gxrInfo);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                Log.WriteAsync("预受理基本信息查询失败:" + ex.Message);
                return false;
            }
        }

    }
}


/// <summary>
/// 预受理预约申请信息查询 
/// </summary>
/// <param name="sfzh"></param>
/// <returns></returns> 
//public bool QueryAppointment(string sfzh, out PreApplyInfo xmlInfo)
//{
//    Serial serial = new Serial();
//    string strXml = serial.GetPersonXmlFile(sfzh);//xml文件路径
//    if (File.Exists(strXml))
//    {
//        File.Delete(strXml);
//    }
//    XmlExplain explain = new XmlExplain();
//    xmlInfo = explain.XmlFileToClass(serial.GetXmpTemplateFile(BaseDirectory), Encoding.UTF8);
//    try
//    {
//        JObject Args = new JObject();
//        Args.Add("sfzh", sfzh);
//        Args.Add("rylb", "R");
//        string json = Args.ToString();
//        WebReturnArgs mReturn = new WebReturnArgs();
//        mReturn = CallUnimethod("sq_queryPreApplyBasic", json, true);
//        Log.WriteAsync("预受理基本信息查询Recv：data" + "[" + mReturn.DataContent + "]");
//        if (!mReturn.CallSuccessed)
//        {
//            Log.WriteAsync("预受理基本信息查询时发生异常:" + mReturn.ExceptionString);
//            return false;
//        }
//        if (!mReturn.Success)
//        {
//            Log.WriteAsync(_strMessage);
//            return false;
//        }
//        JObject jObj = JObject.Parse(mReturn.DataContent);              
//        //标记是否已查询过该种证件
//        bool bIsHaveHZ = false;
//        bool bIsHaveSCZ = false;
//        bool bIsHaveDLZ = false;
//        bool bFingerprint = false;
//        bool bSignature = false;
//        bool bRead = false; //标记是否已经读了基本信息
//        JArray jarray = JArray.Parse(jObj["ndjmxxs"].ToString());
//        if (jarray == null || jarray.Count == 0)
//        {
//            return true;
//        }
//        //筛选出每种证件最新一条，并且sjslrq为空的
//        string zxslrq = "";//最新受理日期
//        foreach (JObject ndjmxxs in jarray)
//        {
//            JObject sq_info = JObject.Parse(ndjmxxs["ndjm"].ToString());
//            string sjslrq = "";
//            if (jObj["sjslrq"] != null)
//            {
//                string sjslrq = jObj["sjslrq"].ToString();
//                if ("" != sjslrq)//实际受理日期,不为空说明已受理
//                    continue;
//            }                  
//            string xxly = sq_info["xxly"].ToString();
//            string sqlb = sq_info["sqlb"].ToString();//101护照 102往来港澳通行证 104往来台湾通行证
//            string bzlb = sq_info["bzlb"].ToString();//81单独签注 
//            string xczjzl = sq_info["xczjzl"].ToString();//14护照 21双程证 25大陆证
//            string ywbh = sq_info["ywbh"].ToString();
//            string slrq = sq_info["slrq"].ToString();//slrq：受理日期。zhxgsj：最后修改时间. yyyyMMddHHmmss 格式
//            if ("08" == xxly || "13" == xxly || "14" == xxly || "15" == xxly && ("" == zxslrq || zxslrq == slrq))//预约数据 "PRE"
//            {
//                xmlInfo.DATA_SOURCE = "PRE";
//                if (!bRead) //还没有读基本信息
//                {
//                    xmlInfo.SFZH = sfzh;
//                    xmlInfo.CSRQ = sq_info["csrq"].ToString();
//                    xmlInfo.ZWX = sq_info["zwx"].ToString();
//                    xmlInfo.ZWM = sq_info["zwm"].ToString();
//                    xmlInfo.YWX = sq_info["ywx"].ToString();
//                    xmlInfo.YWM = sq_info["ywm"].ToString();
//                    xmlInfo.XB.Code = sq_info["xb"].ToString();                           
//                    xmlInfo.MZ.Code = sq_info["mz"].ToString();                           
//                    xmlInfo.CSD.Code = sq_info["csd"].ToString();                          
//                    xmlInfo.HKSZD.Code = sq_info["hkszd"].ToString();                           
//                    xmlInfo.LXDH = sq_info["lxdh"].ToString(); ; //联系电话lxdh
//                    xmlInfo.JJLXR = sq_info["jjqklxr"].ToString(); ; //紧急联系人
//                    xmlInfo.JJLXDH = sq_info["jjqklxrdh"].ToString(); ; //紧急联系电话
//                }

//                if ("" == zxslrq)
//                    zxslrq = slrq;

//                if (!bIsHaveHZ && "101" == sqlb)//只有护照有手写签名
//                {
//                    bIsHaveHZ = true;
//                    bFingerprint = true;
//                    bSignature = true;
//                    bRead = true;
//                    xmlInfo.HZSQ.YWBH = ywbh;
//                    xmlInfo.HZSQ.ZJHM = sq_info["xczjhm"].ToString();                           
//                    xmlInfo.HZSQ.YWLB.Code = bzlb;                           
//                }
//                else if (!bIsHaveSCZ && "102" == sqlb)
//                {
//                    bIsHaveSCZ = true;
//                    bRead = true;

//                    xmlInfo.SCZSQ.YWBH = ywbh;
//                    xmlInfo.SCZSQ.ZJHM = sq_info["xczjhm"].ToString();

//                    if ("92" == bzlb)//单签
//                    {
//                        xmlInfo.SCZSQ.SFQZ.Code = "1";
//                        xmlInfo.SCZSQ.SFQZ.Value = "是";
//                    }
//                    else
//                    {
//                        bFingerprint = true;
//                        xmlInfo.SCZSQ.YWLB.Code = bzlb;
//                    }

//                }
//                else if (!bIsHaveDLZ && "104" == sqlb)
//                {
//                    bIsHaveDLZ = true;
//                    bRead = true;

//                    xmlInfo.DLZSQ.YWBH = ywbh;
//                    xmlInfo.DLZSQ.ZJHM = sq_info["xczjhm"].ToString();

//                    if ("91" == bzlb)//单签
//                    {
//                        xmlInfo.DLZSQ.SFQZ.Code = "1";
//                        xmlInfo.DLZSQ.SFQZ.Value = "是";

//                    }
//                    else
//                    {
//                        bFingerprint = true;
//                        xmlInfo.DLZSQ.YWLB.Code = bzlb;
//                    }
//                }
//            }
//            else
//                continue;
//        }
//    }
//    catch (Exception ex)
//    {
//        Log.WriteException(ex.ToString());
//        Log.WriteAsync("预受理基本信息查询失败:" + ex.Message);
//        return false;
//    }

//    //保存文件
//    explain.ClassToXmlFile(xmlInfo, strXml);
//    if (!File.Exists(strXml))//
//    {
//        Log.WriteAsync(string.Format("保存xml文件失败：{0}", strXml));
//    }
//    return true;
//}


